package de.dhbw.karlsruhe.picsim.model.cpu.impl;

import de.dhbw.karlsruhe.picsim.model.actions.PicAction;
import de.dhbw.karlsruhe.picsim.model.commands.PicInstruction;
import de.dhbw.karlsruhe.picsim.model.cpu.InstructionPipeline;
import de.dhbw.karlsruhe.picsim.model.cpu.PicInstructionFacade;
import de.dhbw.karlsruhe.picsim.model.cpu.Processor;
import de.dhbw.karlsruhe.picsim.model.cpu.execute.ActionExecutor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: goesen
 * Date: Apr 17, 2010
 * Time: 8:57:08 PM
 */
public final class ProcessorImpl implements Processor {
    private static final Log log = LogFactory.getLog(ProcessorImpl.class);
    private final ActionExecutor actionExecutor;
    private final PicInstructionFacade facade;
    private final InstructionPipeline instructionPipeline;


    public ProcessorImpl(final ActionExecutor actionExecutor,
                         final PicInstructionFacade facade,
                         @NotNull final InstructionPipeline instructionPipeline) {

        this.instructionPipeline = instructionPipeline;
        this.actionExecutor = actionExecutor;
        this.facade = facade;
    }


    void execute(final List<? extends PicAction> actions) {
        actionExecutor.executeActions(actions);
    }

    @Override
    public void circle() {
        // quasi q1: hohle den aktuellen befehl
        final PicInstruction currentInstruction = instructionPipeline.popCommand();
        log.info("executing an " + currentInstruction);

        //quasi q2 +q3 : lese Daten und verarbeite Daten
        final List<? extends PicAction> actions = currentInstruction.getActions(facade);

        //quasi a4: schreibe Daten
        execute(actions);
    }


}
